package com.ruoyi.web.qualityController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.quality.Block;
import com.ruoyi.system.service.IBlockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 锁定隔离Controller
 *
 * @author ruoyi
 * @date 2023-03-10
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
        @Api(tags ="锁定隔离")
    @RestController
@RequestMapping("/quality/block" )
public class BlockController extends BaseController {

    private final IBlockService iBlockService;

    @Autowired
    private TokenService tokenService;



    /**
     * 查询锁定隔离列表
     */
    //@PreAuthorize("@ss.hasPermi('quality:block:list')")
    @GetMapping("/list")
        @ApiOperation(value = "查询锁定隔离列表")
        public TableDataInfo list(Block block)
    {
        startPage();
        LambdaQueryWrapper<Block> lqw = new LambdaQueryWrapper<Block>();
        if (StringUtils.isNotBlank(block.getMaterial())){
            lqw.like(Block::getMaterial ,block.getMaterial());
        }
        if (StringUtils.isNotBlank(block.getCode())){
            lqw.like(Block::getCode ,block.getCode());
        }
        if (block.getStatus() != null){
            lqw.eq(Block::getStatus ,block.getStatus());
        }
        if (StringUtils.isNotBlank(block.getGysCode())){
            lqw.like(Block::getGysCode ,block.getGysCode());
        }
        if (StringUtils.isNotBlank(block.getNineCode())){
            lqw.like(Block::getNineCode ,block.getNineCode());
        }
        if (StringUtils.isNotBlank(block.getPsn())){
            lqw.like(Block::getPsn ,block.getPsn());
        }
        if (StringUtils.isNotBlank(block.getCreateByName())){
            lqw.apply("b.user_name like '%"+block.getCreateByName()+"%'");
        }
        if (StringUtils.isNotBlank(block.getUpdateByName())){
            lqw.apply("c.user_name like '%"+block.getUpdateByName()+"%'");
        }
        List<Block> list = iBlockService.listByParam(lqw);
        return getDataTable(list);
    }

    /**
     * 导出锁定隔离列表
     */
    //@PreAuthorize("@ss.hasPermi('quality:block:export')" )
    @Log(title = "锁定隔离" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
                @ApiOperation(value = "导出锁定隔离列表")
            public AjaxResult export(Block block) {
        LambdaQueryWrapper<Block> lqw = new LambdaQueryWrapper<Block>(block);
        List<Block> list = iBlockService.list(lqw);
        ExcelUtil<Block> util = new ExcelUtil<Block>(Block. class);
        return util.exportExcel(list, "block" );
    }

    /**
     * 获取锁定隔离详细信息
     */
    //@PreAuthorize("@ss.hasPermi('quality:block:query')" )
    @GetMapping(value = "/{id}" )
                @ApiOperation(value = "获取锁定隔离详细信息")
            public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iBlockService.getById(id));
    }

    /**
     * 新增锁定隔离
     */
    //@PreAuthorize("@ss.hasPermi('quality:block:add')" )
    @Log(title = "锁定隔离" , businessType = BusinessType.INSERT)
    @PostMapping
                @ApiOperation(value = "新增锁定隔离")
            public AjaxResult add(@RequestBody Block block) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        block.setCreateTime(new Date());
        block.setCreateBy(loginUser.getUser().getUserId());
        block.setUpdateTime(new Date());
        block.setUpdateBy(loginUser.getUser().getUserId());
        return toAjax(iBlockService.save(block) ? 1 : 0);
    }

    /**
     * 修改锁定隔离
     */
    //@PreAuthorize("@ss.hasPermi('quality:block:edit')" )
    @Log(title = "锁定隔离" , businessType = BusinessType.UPDATE)
    @PutMapping
                @ApiOperation(value = "修改锁定隔离")
            public AjaxResult edit(@RequestBody Block block) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        block.setUpdateTime(new Date());
        block.setUpdateBy(loginUser.getUser().getUserId());
        return toAjax(iBlockService.updateById(block) ? 1 : 0);
    }

    /**
     * 删除锁定隔离
     */
    //@PreAuthorize("@ss.hasPermi('quality:block:remove')" )
    @Log(title = "锁定隔离" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
                @ApiOperation(value = "删除锁定隔离")
            public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iBlockService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
